<template>
  <div :id="chartid" style="width: 100%; height: 100%"></div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import * as echarts from "echarts";

// 接收组件传来的参数
const props = defineProps({
  chartid: {
    type: String
  },
  xData: {
    type: Array,
    default: [], //默认值
  },
  barData: {
    type: Array,
    default: [], //默认值
  },
  lineData: {
    type: Array,
    default: [], //默认值
  }
});
//声明周期函数，自动执行初始化
onMounted(() => {
  init(props.chartid, props.xData, props.barData, props.lineData);
});
function getImageUrl (name) {
  return new URL(`../assets/images/${name}`, import.meta.url).href;
}

//初始化函数
function init (id, xData, barData, lineData) {
  let that = this;
  // 基于准备好的dom，初始化echarts实例
  let Chart = echarts.init(document.getElementById(id));
  let seriesObj = [];
  if (barData && barData.length) {
    seriesObj.push({
      barWidth: 14,
      name: '计划投资',
      type: 'bar',
      // barGap: '-100%',
      itemStyle: {
        color: new echarts.graphic.LinearGradient(
          0, 0, 0, 1,
          [
            { offset: 0, color: 'rgba(25, 174, 255, 1)' },

            { offset: 1, color: 'rgba(163, 223, 255, 1)' }
          ]
        ),
        borderRadius: [7, 7, 0, 0]
      },

      data: barData
    })
  }

  if (lineData && lineData.length) {
    seriesObj.push({
      name: '项目个数',
      type: 'line',
      smooth: true,
      symbol: 'none',     //设定为实心点
      symbolSize: 8,   //设定实心点的大小
      yAxisIndex: 1,
      color: "rgba(0, 255, 246, 1)",
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [{
            offset: 0, color: 'rgba(0, 255, 246, 0.5)'
          }, {
            offset: 1, color: 'rgba(0, 255, 246, 0)'
          }],
          global: false
        }
      },
      label: {
        show: false,
      },
      data: lineData
    })
  }

  // 绘制图表
  let options = {

    tooltip: {
      show: false,
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#283b56'
        }
      }
    },
    legend: {
      show: true,
      top: '0%',
      right: '12%',
      data: [{
        name: '计划投资',
        itemStyle: {
          color: "#1dafff",
          borderWidth: 7,
          borderColor: 'rgba(29, 177, 255,.4)',
        }
      }, {
        name: '项目个数',
        itemStyle: {
          color: "#00fff6",
          borderWidth: 7,
          borderColor: 'rgba(0, 255, 246,.4)',
        }
      }],
      icon: 'circle',
      itemGap: 24,
      itemWidth: 6,
      itemHeight: 6,
      textStyle: {
        fontSize: 14,
        color: '#fff'
      }
    },
    grid: {
      left: "4%",
      right: "4%",
      bottom: "0%",
      top: '9.5%',
      containLabel: true
    },

    yAxis: [


      {
        type: 'value',
        name: "万元",
        nameTextStyle: {
          color: "#1dafff",
          fontSize: 14,
          align: 'right'
        },
        axisLabel: {
          color: '#FFF',
          fontSize: '14',
        },
        axisTick: {
          show: false,    // 是否显示坐标轴刻度
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: 'rgba(255,255,255,0.3)',
            // type: 'dashed'
          },
        },

      },


      {

        show: true,
        type: 'value',
        alignTicks: true,//对齐刻度线
        name: "个数",
        nameTextStyle: {
          color: "#fff",
          fontSize: 14,
          align: 'left'
        },
        axisTick: {
          show: false
        },

        axisLine: {
          show: false
        },
        axisLabel: {
          fontSize: 14,
          color: "#fff",

        },
        splitLine: {
          show: false,
        }


      }
    ],

    xAxis: [

      {
        type: 'category',

        show: true,
        axisTick: {
          show: false,    // 是否显示坐标轴刻度
        },
        axisLabel: {
          color: '#FFF',
          fontSize: '14',
          interval: 0,
        },

        data: xData



      }
    ],

    series: seriesObj
  };
  // 渲染图表
  Chart.setOption(options);
  window.addEventListener('resize', function () {
    Chart.resize();
  }, false);
}
</script>